package com.csmall.stock.webapi.service.Impl;

import com.csmall.commons.exception.CoolSharkServiceException;
import com.csmall.commons.pojo.stock.dto.StockReduceCountDTO;
import com.csmall.commons.restful.ResponseCode;
import com.csmall.stock.service.StockService;
import com.csmall.stock.webapi.mapper.StockMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Name: StockServiceImpl
 * @Author: 西风残叶
 * @Date: 2024/8/18 19:07
 * @Version: 1.0
 * @TODO: TODO
 */

@DubboService
@Service
@Slf4j
public class StockServiceImpl implements StockService {
    @Autowired
    private StockMapper stockMapper;

    @Override
    public void reduceStockCount(StockReduceCountDTO stockReduceCountDTO) {

        int row=stockMapper.updateStockCount(
                stockReduceCountDTO.getCommodityCode(),
                stockReduceCountDTO.getReduceCount());
        // 判断row的值,如果为0,抛出异常
        if(row==0){
            throw new CoolSharkServiceException(ResponseCode.BAD_REQUEST,"库存不足");
        }
        log.info("库存减少完成!");
    }
}